package com.zoe.onelink.core.web.validation;

import com.zoe.onelink.core.web.validation.resolver.ValidationFieldNameResolver;
import com.zoe.onelink.util.StrUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.Errors;
import org.springframework.validation.beanvalidation.CustomValidatorBean;

import javax.validation.ConstraintViolation;
import java.util.Set;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-11-30
 */
@RequiredArgsConstructor
public class OnelinkParamValidator extends CustomValidatorBean {

    private final ValidationFieldNameResolver validationFieldNameResolver;

    @Override
    protected void processConstraintViolations(Set<ConstraintViolation<Object>> violations, Errors errors) {
        super.processConstraintViolations(violations, errors);
    }

    @Override
    protected String determineField(ConstraintViolation<Object> violation) {
        String fieldName = super.determineField(violation);
        if (this.validationFieldNameResolver == null || StrUtil.isBlank(fieldName)) {
            return fieldName;
        }
        return this.validationFieldNameResolver.resolve(violation.getRootBeanClass().getName(), fieldName);
    }
}
